package com.netposa.datawarehouse.aggregator.client.search;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.netposa.appwebres.common.WebResInfo;
import com.netposa.datawarehouse.aggregator.client.NonmotorServiceClient;
import com.netposa.datawarehouse.aggregator.client.fallback.BaseServiceClientFallback;
import com.netposa.datawarehouse.aggregator.search.ResourceParam;
import com.netposa.datawarehouse.aggregator.search.ResourceResult;
import com.netposa.datawarehouse.aggregator.web.model.DefaultCondition;
import com.netposa.library.datawarehouse.aggregate.helper.ResourceDefined.ResourceInnerFiled;
import com.netposa.library.datawarehouse.aggregate.helper.ResourceDefined.ResourceType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;

@Component("nonmotor")
public class NonmotorResourceSearch extends AbstractResourceSearch {
   private static final Logger log = LoggerFactory.getLogger(NonmotorResourceSearch.class);
   @Autowired
   private Gson gson;
   @Autowired
   private NonmotorServiceClient nonmotorServiceClient;
   @Autowired
   private BaseServiceClientFallback serviceClientFallback;

   public List<ResourceResult> search(List<ResourceParam> resourceParams) {
      if (resourceParams != null && !resourceParams.isEmpty()) {
         String recordIds = Joiner.on(",").skipNulls().join(Lists.transform(resourceParams, (input) -> {
            return input == null ? null : input.getRecordId();
         }));
         NonmotorServiceClient.NonmotorCondition condition = new NonmotorServiceClient.NonmotorCondition();
         condition.setRecordIds(recordIds);
         WebResInfo webResInfo = this.nonmotorServiceClient.getNonmotoByRecordIds(condition);
         if (webResInfo.getCode().equalsIgnoreCase("200") && webResInfo.getData() != null) {
            String dataJson = this.gson.toJson(webResInfo.getData());
            List<Map<String, Object>> mapList = (List)this.gson.fromJson(dataJson, (new TypeToken() {
            }).getType());
            List resultList = Lists.transform(mapList, (input) -> {
               ResourceResult result = new ResourceResult();
               result.setData(input);
               result.getData().put(ResourceInnerFiled._TYPE.getFiled(), ResourceType.NONMOTOR.getValue());
               result.getData().put(ResourceInnerFiled._RECORD_ID.getFiled(), result.getData().get("recordId"));
               result.getData().put(ResourceInnerFiled._ABS_TIME.getFiled(), result.getData().get("absTime"));
               result.getData().put(ResourceInnerFiled.SOURCE.getFiled(), result.getData().get("source"));
               return result;
            });
            return resultList;
         } else {
            log.error("Request face server failed {} = {} .", webResInfo.getCode(), webResInfo.getMessage());
            return new ArrayList();
         }
      } else {
         return new ArrayList();
      }
   }

   public List selectInParams(DefaultCondition defaultCondition) {
      WebResInfo webResInfo = this.nonmotorServiceClient.getNonmotorList(JSON.toJSONString(defaultCondition));
      return this.rebuild(webResInfo, ResourceType.NONMOTOR.getValue());
   }

   public List<ResourceResult> selectInParams2(RequestMethod requestMethod, String serviceName, String typeName, String path, String requestBody) {
      WebResInfo webResInfo = this.serviceClientFallback.handler(requestMethod, serviceName, path, requestBody);
      return this.rebuild(webResInfo, typeName);
   }

   public Integer count(DefaultCondition defaultCondition) {
      WebResInfo webResInfo = this.nonmotorServiceClient.countNonmotor(JSON.toJSONString(defaultCondition));
      if (webResInfo.getCode().equalsIgnoreCase("200") && webResInfo.getData() != null) {
         Double count = Double.valueOf(String.valueOf(webResInfo.getData()));
         return count.intValue();
      } else {
         log.error("Request face server failed {} = {} .", webResInfo.getCode(), webResInfo.getMessage());
         return 0;
      }
   }
}
